package com.beiben.project.controller;

import com.beiben.common.GlobalConstant;
import com.beiben.project.domain.Project;
import com.beiben.project.service.IProjectService;
import com.beiben.system.file.domain.File;
import com.beiben.system.file.service.IFileService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 项目管理Controller
 *
 * @author 熊国强
 * @date 2020-12-03
 */
@RestController
@RequestMapping("/project/project/tree")
public class ProjectTreeController extends BaseController {
    @Autowired
    private IProjectService projectService;

    @Autowired
    private IFileService fileService;


    /**
     * 查询项目管理列表
     */
    @PreAuthorize("@ss.hasPermi('project:project:list')")
    @GetMapping("/list")
    public AjaxResult list(Project project) {
        List<Project> list = projectService.selectProjectList(project);
        return AjaxResult.success(list);
    }

    /**
     * 导出项目管理列表
     */
    @PreAuthorize("@ss.hasPermi('project:project:export')")
    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Project project) {
        List<Project> list = projectService.selectProjectList(project);
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        return util.exportExcel(list, "project");
    }

    /**
     * 获取项目管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:project:query')")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") Long projectId) {
        return AjaxResult.success(projectService.selectProjectById(projectId));
    }

    /**
     * 新增项目管理
     */
    @PreAuthorize("@ss.hasPermi('project:project:add')")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Project project) {
        return toAjax(projectService.insertProject(project));
    }

    /**
     * 修改项目管理
     */
    @PreAuthorize("@ss.hasPermi('project:project:edit')")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Project project) {
        return toAjax(projectService.updateProject(project));
    }

    /**
     * 删除项目管理
     */
    @PreAuthorize("@ss.hasPermi('project:project:remove')")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable Long[] projectIds) {
        return toAjax(projectService.deleteProjectByIds(projectIds));
    }


    /**
     * 获取项目下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(@RequestParam("relParentId") String relParentId) {
        return AjaxResult.success(projectService.selectProjectTreeList(relParentId));
    }

    /**
     * 获取顶级数据
     */
    @GetMapping("/parentTree")
    public AjaxResult parentTreeSelect(@RequestParam("relParentId") String relParentId, @RequestParam("isParent") String isParent) {
        return AjaxResult.success(projectService.selectProjectParentTreeList(relParentId, isParent));
    }

    /**
     * 上传附件
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile[] file, Long tableId) {
        try {
            fileService.uploadFile(file, tableId, GlobalConstant.PM_PROJECT, GlobalConstant.PM_PROJECT_ADDRESS);
            AjaxResult ajax = AjaxResult.success();
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取文件列表
     *
     * @return
     */
    @PostMapping("/files/{projectId}")
    public TableDataInfo selectFiles(@PathVariable Long projectId) {
        startPage();
        List<File> list = projectService.selectFiles(projectId);
        return getDataTable(list);
    }

    /**
     * 项目经理直接完成工作
     *
     * @return
     */
    @Log(title = "审批")
    @PostMapping("/approveComplete")
    public AjaxResult approveCompleteProject(Long[] projectIds, Long[] itemIds) {
        // TODO: 2021-01-19 做权限验证
        int result = projectService.approveComplete(projectIds, itemIds);
        return toAjax(result);
    }

}
